﻿package mortal.game.scene3D.map3D.AstarAnyDirection.processor
{
    import __AS3__.vec.*;
    import flash.geom.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;

    public class MapGridTurnPointMaker extends Object
    {
        private static var count:int = 0;
        private static var testP1:AstarTurnPoint = new AstarTurnPoint();
        private static var testP2:AstarTurnPoint = new AstarTurnPoint();
        private static var _path:Array;
        private static var test:AstarTurnPoint = new AstarTurnPoint();

        public function MapGridTurnPointMaker()
        {
            return;
        }// end function

        public static function work2(param1:Array, param2:Point, param3:Point) : Array
        {
            var _loc_5:AstarIILink = null;
            var _loc_4:* = new AstarTurnPoint();
            _loc_4._x = param2.x;
            _loc_4._y = param2.y;
            _loc_5 = new AstarIILink();
            _loc_5.minCol = param3.x;
            _loc_5.minRow = param3.y;
            _loc_5.maxCol = param3.x;
            _loc_5.maxRow = param3.y;
            param1.push(_loc_5);
            _loc_5 = param1[0];
            var _loc_6:* = new AstarTurnPoint();
            _loc_6.setValue(_loc_5.minCol, _loc_5.minRow);
            var _loc_7:* = new AstarTurnPoint();
            _loc_7.setValue(_loc_5.maxCol, _loc_5.maxRow);
            var _loc_8:* = new AstarTurnPoint();
            var _loc_9:* = new AstarTurnPoint();
            var _loc_10:Object = {};
            var _loc_11:int = 1;
            while (_loc_11 < param1.length)
            {
                
                _loc_5 = param1[_loc_11];
                _loc_8.setValue(_loc_5.minCol, _loc_5.minRow);
                _loc_9.setValue(_loc_5.maxCol, _loc_5.maxRow);
                addNewLink(_loc_4, _loc_6, _loc_7, _loc_8, _loc_9, _loc_10);
                _loc_4 = _loc_10.P;
                _loc_6 = _loc_10.A;
                _loc_7 = _loc_10.B;
                _loc_11++;
            }
            var _loc_12:* = new AstarTurnPoint();
            _loc_12.x = param3.x;
            _loc_12.y = param3.y;
            _loc_12.parent = _loc_4;
            buildPath(_loc_12);
            return _path;
        }// end function

        private static function addNewLink(param1:AstarTurnPoint, param2:AstarTurnPoint, param3:AstarTurnPoint, param4:AstarTurnPoint, param5:AstarTurnPoint, param6:Object) : void
        {
            var _loc_9:Boolean = false;
            var _loc_10:Boolean = false;
            var _loc_11:Number = NaN;
            var _loc_12:Number = NaN;
            if (param2._x == param3._x && param2._y == param3._y)
            {
                param2.parent = param1;
                param1 = param2;
                param2 = new AstarTurnPoint();
                param2.setValue(param4._x, param4._y);
                param3.setValue(param5._x, param5._y);
                param6.P = param1;
                param6.A = param2;
                param6.B = param3;
                return;
            }
            var _loc_7:* = GeomUtil.isLimitLineCross(param4, param1, param2, param3);
            var _loc_8:* = GeomUtil.isLimitLineCross(param5, param1, param2, param3);
            if (_loc_7 && _loc_8)
            {
                param2.setValue(param4._x, param4._y);
                param3.setValue(param5._x, param5._y);
                param6.P = param1;
                param6.A = param2;
                param6.B = param3;
                return;
            }
            if (!_loc_7 && !_loc_8)
            {
                _loc_9 = GeomUtil.isLineP3P4CrossSegmentsP1P2(param4, param5, param1, param2);
                _loc_10 = GeomUtil.isLineP3P4CrossSegmentsP1P2(param4, param5, param1, param3);
                if (_loc_9 && _loc_10)
                {
                    GeomUtil.calcLineCrossPoint(param1, param2, param4, param5, testP1);
                    GeomUtil.calcLineCrossPoint(param1, param3, param4, param5, testP2);
                    param2.setValue(testP1._x, testP1._y);
                    param3.setValue(testP2._x, testP2._y);
                    param6.P = param1;
                    param6.A = param2;
                    param6.B = param3;
                    return;
                }
                _loc_11 = GeomUtil.calcDistance2(param1, param2) + GeomUtil.calcDistance2(param2, param4);
                _loc_12 = GeomUtil.calcDistance2(param1, param3) + GeomUtil.calcDistance2(param3, param4);
                if (_loc_11 < _loc_12)
                {
                    param2.parent = param1;
                    param1 = param2;
                    param2 = new AstarTurnPoint();
                    param2.setValue(param4._x, param4._y);
                    param3.setValue(param5._x, param5._y);
                }
                else if (_loc_11 > _loc_12)
                {
                    param3.parent = param1;
                    param1 = param3;
                    param3 = new AstarTurnPoint();
                    param3.setValue(param5._x, param5._y);
                    param2.setValue(param4._x, param4._y);
                }
            }
            else
            {
                if (GeomUtil.isLineP3P4CrossSegmentsP1P2(param4, param5, param1, param2))
                {
                    GeomUtil.calcLineCrossPoint(param1, param2, param4, param5, testP1);
                    param3.setValue(testP1._x, testP1._y);
                }
                else if (GeomUtil.isLineP3P4CrossSegmentsP1P2(param4, param5, param1, param3))
                {
                    GeomUtil.calcLineCrossPoint(param1, param3, param4, param5, testP2);
                    param3.setValue(testP2._x, testP2._y);
                }
                if (_loc_7)
                {
                    param2.setValue(param4._x, param4._y);
                }
                else if (_loc_8)
                {
                    param2.setValue(param5._x, param5._y);
                }
            }
            param6.P = param1;
            param6.A = param2;
            param6.B = param3;
            return;
        }// end function

        private static function buildPath(param1:AstarTurnPoint) : void
        {
            _path = [];
            while (param1)
            {
                
                _path.unshift(param1);
                param1 = param1.parent;
            }
            return;
        }// end function

        private static function addNewPoint(param1:AstarTurnPoint, param2:Array, param3:int, param4:int) : int
        {
            var _loc_5:int = 0;
            var _loc_8:AstarTurnPoint = null;
            var _loc_9:Number = NaN;
            var _loc_6:* = param4;
            var _loc_7:Number = 100000000;
            test.from(param1);
            _loc_5 = param3;
            while (_loc_5 <= param4)
            {
                
                _loc_8 = param2[_loc_5];
                if (isLineVisible(_loc_8, param1, param2, _loc_5 * 0.5 + 1, param4 * 0.5))
                {
                    _loc_9 = _loc_8.distance + GeomUtil.calcDistance2(test, _loc_8);
                    if (_loc_9 < _loc_7)
                    {
                        _loc_7 = _loc_9;
                        _loc_6 = _loc_5;
                        param1.parent = _loc_8;
                    }
                }
                _loc_5++;
            }
            if (param1.parent == null)
            {
                param1.distance = 0;
            }
            else
            {
                param1.distance = param1.parent.distance + GeomUtil.calcDistance2(param1, param1.parent);
            }
            return _loc_6;
        }// end function

        private static function isLineVisible(param1:AstarTurnPoint, param2:AstarTurnPoint, param3:Array, param4:int, param5:int) : Boolean
        {
            var _loc_6:int = 0;
            var _loc_7:AstarTurnPoint = null;
            var _loc_8:AstarTurnPoint = null;
            _loc_6 = param4;
            while (_loc_6 <= param5)
            {
                
                _loc_7 = param3[_loc_6 * 2];
                _loc_8 = param3[_loc_6 * 2 + 1];
                var _loc_10:* = count + 1;
                count = _loc_10;
                if (!GeomUtil.isLineP3P4CrossSegmentsP1P2(_loc_7, _loc_8, param1, param2))
                {
                    return false;
                }
                _loc_6 = _loc_6 + 1;
            }
            return true;
        }// end function

        public static function work(param1:Array, param2:Point, param3:Point) : Array
        {
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_12:AstarTurnPoint = null;
            var _loc_13:AstarTurnPoint = null;
            var _loc_4:* = param1[0];
            if (lineWalkable(param2.x, param2.y, param3.x, param3.y))
            {
                _path = [];
                _loc_13 = new AstarTurnPoint();
                _loc_13._x = param2.x;
                _loc_13._y = param2.y;
                _path.push(_loc_13);
                _loc_13 = new AstarTurnPoint();
                _loc_13._x = param3.x;
                _loc_13._y = param3.y;
                _path.push(_loc_13);
                return _path;
            }
            var _loc_5:* = new AstarIILink();
            _loc_5.minCol = param2.x;
            _loc_5.minRow = param2.y;
            _loc_5.maxCol = param2.x;
            _loc_5.maxRow = param2.y;
            if (_loc_4 != null && _loc_4.minCol == param2.x && _loc_4.minRow == param2.y && _loc_4.maxCol == param2.x && _loc_4.maxRow == param2.y)
            {
            }
            else
            {
                param1.unshift(_loc_5);
            }
            _loc_5 = new AstarIILink();
            _loc_5.minCol = param3.x;
            _loc_5.minRow = param3.y;
            _loc_5.maxCol = param3.x;
            _loc_5.maxRow = param3.y;
            param1.push(_loc_5);
            var _loc_6:Array = [];
            var _loc_9:int = -1;
            var _loc_10:int = -2;
            var _loc_11:int = 0;
            count = 0;
            _loc_7 = 0;
            while (_loc_7 < param1.length)
            {
                
                _loc_5 = param1[_loc_7];
                _loc_12 = new AstarTurnPoint();
                _loc_12.setValue(_loc_5.minCol, _loc_5.minRow);
                if (_loc_5.isJumpLink)
                {
                    _loc_12.isJump = true;
                }
                _loc_6.push(_loc_12);
                _loc_9 = -1;
                _loc_10 = -2;
                _loc_9 = addNewPoint(_loc_12, _loc_6, _loc_11, _loc_7 * 2 - 1);
                _loc_12 = new AstarTurnPoint();
                _loc_12.setValue(_loc_5.maxCol, _loc_5.maxRow);
                if (_loc_5.isJumpLink)
                {
                    _loc_12.isJump = true;
                }
                _loc_6.push(_loc_12);
                _loc_10 = addNewPoint(_loc_12, _loc_6, _loc_11, _loc_7 * 2 - 1);
                if (_loc_9 == _loc_10 && _loc_9 >= 0)
                {
                    _loc_11 = _loc_9;
                }
                else if (GeomUtil.calcDistance(_loc_5.minCol, _loc_5.minRow, _loc_5.maxCol, _loc_5.maxRow) <= 5)
                {
                    _loc_11 = Math.max(_loc_7 * 2 - 2, 0);
                }
                _loc_7++;
            }
            buildPath(_loc_6[_loc_6.length - 2]);
            return _path;
        }// end function

        public static function lineWalkable(param1:int, param2:int, param3:int, param4:int) : Boolean
        {
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_5:* = GeomUtil.calcLinePassGridesFastMidle(param1, param2, param3, param4);
            var _loc_6:* = AstarAnyDirection.mapData;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_5.length)
            {
                
                _loc_8 = int(_loc_5[_loc_7]);
                _loc_9 = int(_loc_5[(_loc_7 + 1)]);
                _loc_10 = _loc_6[_loc_9][_loc_8];
                if (!MapGridCutter.walkable(_loc_10))
                {
                    return false;
                }
                _loc_7 = _loc_7 + 2;
            }
            return true;
        }// end function

    }
}
